package agents;

import agents.profileragentbehaviuors.GetTourFromGuideBehaviour;
import agents.profileragentbehaviuors.WalkTourBehaviour;
import resource.UserProfile;
import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.OneShotBehaviour;
import jade.core.behaviours.SequentialBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.SearchConstraints;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

public class ProfilerAgent extends Agent{
	UserProfile user;
	AID tourGuide;
	String[] tour = null;
	
	@Override
	protected void setup() {
		user = new UserProfile();
		user.setAge(20);
		user.setGender("Man");
		user.setOccupation("Engineer");
		user.addInterest("Painting");
		addBehaviour(new RegisterTourSubscription());
		addBehaviour(new RegistrationNotificationBehaviour());
	}
	
	public void setTourGuide(AID tourGuide){
		this.tourGuide = tourGuide;
	}
	
	public void setTour(String[] tour){
		this.tour = tour;
	}
	
	public AID getTourGuide(){
		return tourGuide;
	}
	
	class RegistrationNotificationBehaviour extends CyclicBehaviour{

		@Override
		public void action() {
			ACLMessage msg = receive(MessageTemplate.MatchSender(getDefaultDF()));
	                
			if (msg != null){
				try{
					DFAgentDescription[] dfds = DFService.decodeNotification(msg.getContent());
				    if (dfds.length > 0){
				    	tourGuide = dfds[0].getName();
				    	myAgent.addBehaviour(new GetTour(ProfilerAgent.this));
				    	System.out.println(myAgent.getLocalName() + " got 'get tour' service notification, tour guide: " + tourGuide.getLocalName());
				 	}
				}catch (Exception ex) {}
			}
			block();
		}
	}
	
	class RegisterTourSubscription extends OneShotBehaviour{

		@Override
		public void action() {
			DFAgentDescription dfd = new DFAgentDescription();
		      ServiceDescription sd = new ServiceDescription();
		      sd.setType("get tour");
		      dfd.addServices(sd);
		      SearchConstraints sc = new SearchConstraints();
		      sc.setMaxResults(new Long(1));
		      send(DFService.createSubscriptionMessage(myAgent, getDefaultDF(), dfd, sc));
		      System.out.println(myAgent.getLocalName() + " is now subscribing to 'get tour' service");
		}
	}
	
	class GetTour extends OneShotBehaviour{
		ProfilerAgent owner;
		
		public GetTour(ProfilerAgent owner) {
			super(owner);
			this.owner = owner;
		}

		@Override
		public void action() {
			SequentialBehaviour getTour = new SequentialBehaviour();
			getTour.addSubBehaviour(new GetTourFromGuideBehaviour(user, owner));
			getTour.addSubBehaviour(new WalkTourBehaviour(owner));
			owner.addBehaviour(getTour);
		}
	}
	
	class GetArtifacts extends CyclicBehaviour{

		@Override
		public void action() {
			if(tour != null){
				System.out.println(tour);
			}
		}
	}

	public String[] getTour() {
		return tour;
	}

	public UserProfile getUserInfo() {
		return user;
	}
}
